#! /usr/bin/perl

##  Copyright 2007 Andrew Kreling
##  
##  Licensed under the Apache License, Version 2.0 (the "License");
##  you may not use this file except in compliance with the License.
##  You may obtain a copy of the License at
##  
##      http://www.apache.org/licenses/LICENSE-2.0
##  
##  Unless required by applicable law or agreed to in writing, software
##  distributed under the License is distributed on an "AS IS" BASIS,
##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##  See the License for the specific language governing permissions and
##  limitations under the License.

use strict;
use warnings;

my $base_dir_env_var = "THE_CYCLE_TODO_BASE_DIR";

my $bad_args_error = "usage: newfile [-d] name\n";
my $bad_env_error = "$base_dir_env_var is undefined, did you source source_me.sh?\n";

my $file_name = "";
my $dont_add_flag = "-d";
my $dont_add = 0;

if (!defined($ENV{$base_dir_env_var})) {
   die $bad_env_error;
}
my $base_dir = $ENV{$base_dir_env_var};

while (my $current_arg = shift @ARGV) {
  if ($file_name ne "") {
    die($bad_args_error);
  }

  if ($current_arg eq $dont_add_flag) {
    $dont_add = 1;
  }
  else {
    $file_name = $current_arg;
  }
}

my $which_license = ($file_name =~ /\.java$/) ? "_java" : "_script";

my $license_loc = $base_dir . "/doc/fileheader$which_license";
system "cp $license_loc $file_name";
if ($dont_add == 0) {
  system "svn add $file_name";
}
